package com.leostadyn.stackfarm.timestatistics.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.leostadyn.stackfarm.timestatistics.R;
import com.leostadyn.stackfarm.timestatistics.model.bean.StaffTimeRecordBean;
import com.leostadyn.stackfarm.timestatistics.utils.SwipeMenuLayout;
import com.leostadyn.stackfarm.timestatistics.utils.TimeUtils;

import java.util.List;

public class TimeRecordAdapter extends RecyclerView.Adapter {
    private Context mContext;
    private List<StaffTimeRecordBean.DataListBean> dataListBeans;
    private OnDeleteButtonClick onDeleteButtonClick;
    private OnEditButtonClick onEditButtonClick;
    private boolean swipe = true;

    public TimeRecordAdapter(Context mContext, List<StaffTimeRecordBean.DataListBean> dataListBeans,
                             OnDeleteButtonClick onDeleteButtonClick,
                             OnEditButtonClick onEditButtonClick) {
        this.mContext = mContext;

        if (dataListBeans != null) {
            this.dataListBeans = dataListBeans;
        }
        this.onDeleteButtonClick = onDeleteButtonClick;
        this.onEditButtonClick = onEditButtonClick;
    }

    public void setDataListBeans(List<StaffTimeRecordBean.DataListBean> dataListBeans) {

        if (dataListBeans != null && dataListBeans.size() > 0) {
            this.dataListBeans.clear();
            this.dataListBeans.addAll(dataListBeans);
        }
        notifyDataSetChanged();
    }

    public boolean isSwipe() {
        return swipe;
    }

    public void setSwipe(boolean swipe) {
        this.swipe = swipe;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_time_record, viewGroup, false);
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {

        if (viewHolder instanceof MyViewHolder) {
            StaffTimeRecordBean.DataListBean data = dataListBeans.get(i);
            ((MyViewHolder) viewHolder).timeRecordName.setText(data.getProjectName());
            ((MyViewHolder) viewHolder).timeRecordId.setText(data.getProjectCode());
            ((MyViewHolder) viewHolder).timeRecordCreatedTime.setText(TimeUtils.getTime2(data.getWorkDate()));
            ((MyViewHolder) viewHolder).timeRecordConsumeTime.setText("耗时 " + data.getTaskTime() + " 天");

            ((MyViewHolder) viewHolder).swipeMenuLayout.setSwipeEnable(swipe);

            ((MyViewHolder) viewHolder).timeRecordEdit.setOnClickListener(v -> {
                onEditButtonClick.onEditButtonClick(data);
            });

            ((MyViewHolder) viewHolder).timeRecordDelete.setOnClickListener(v -> {
                onDeleteButtonClick.onBtnClicked(data);
            });
        }

    }

    @Override
    public int getItemCount() {
        if (dataListBeans != null) {
            return dataListBeans.size();
        }
        return 0;
    }

    public interface OnEditButtonClick {
        void onEditButtonClick(StaffTimeRecordBean.DataListBean data);
    }

    public interface OnDeleteButtonClick {
        void onBtnClicked(StaffTimeRecordBean.DataListBean dataListBean);
    }


    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView timeRecordName;
        TextView timeRecordId;
        TextView timeRecordCreatedTime;
        TextView timeRecordConsumeTime;
        TextView timeRecordEdit;
        TextView timeRecordDelete;
        SwipeMenuLayout swipeMenuLayout;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);

            timeRecordName = itemView.findViewById(R.id.tv_time_record_name);

            timeRecordId = itemView.findViewById(R.id.tv_time_record_id);

            timeRecordCreatedTime = itemView.findViewById(R.id.tv_record_time_time);

            timeRecordConsumeTime = itemView.findViewById(R.id.tv_time_record_id_consume_time);

            timeRecordEdit = itemView.findViewById(R.id.tv_record_time_edit);

            timeRecordDelete = itemView.findViewById(R.id.tv_record_time_delete);

            swipeMenuLayout = itemView.findViewById(R.id.swipe);


        }
    }
}
